package com.jn.glide.views;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CenterInside;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.GranularRoundedCorners;
import com.bumptech.glide.load.resource.bitmap.Rotate;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.transition.DrawableCrossFadeFactory;
import com.jn.glide.R;
import com.jn.glide.base.BaseActivity;
import com.jn.glide.contract.MainContract;
import com.jn.glide.entity.EnglishEntity;
import com.jn.glide.presenter.MainPresenter;

import java.security.MessageDigest;

import jp.wasabeef.glide.transformations.BitmapTransformation;
import jp.wasabeef.glide.transformations.BlurTransformation;
import jp.wasabeef.glide.transformations.GrayscaleTransformation;

public class MainActivity extends BaseActivity implements MainContract.IMainView, View.OnClickListener {

    private Button mButton;
    private TextView mText;
    private ImageView mImage;
    private ImageView mImage2;
    private MainContract.IMainPresenter mPresenter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void init() {
        mPresenter = new MainPresenter(this, this);
        mButton = findViewById(R.id.button);
        mText = findViewById(R.id.text);
        mImage = findViewById(R.id.image);
        mImage2 = findViewById(R.id.image2);
        mButton.setOnClickListener(this);

        findViewById(R.id.button01).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, OKHttpPicassoActivity.class));
            }
        });
        findViewById(R.id.button02).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, RetrofitGlideActivity.class));
            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.button) {
            mPresenter.getData();
        }
    }

    @Override
    public void onHttpLoading(boolean loading) {

    }

    @Override
    public void onHttpFail(String... msg) {

    }

    @Override
    public void onHttpTimeout() {

    }

    @Override
    public void onHttpTokenExpiration() {

    }

    @Override
    public void showData(EnglishEntity english) {
        StringBuffer sb = new StringBuffer();
        sb.append(english.getResult().getNote()).append("\n\n");
        sb.append(english.getResult().getContent());
        mText.setText(sb.toString());
        Glide.with(this)
                .load(english.getResult().getImg())
                //.dontAnimate() //不显示动画
                //.placeholder(R.mipmap.loding) // 占位图
                //.error(R.mipmap.error) //错误时显示的图
                //.fallback(R.drawable.ic_launcher_background)//请求url为null时的占位图
                //.circleCrop() // 圆形裁剪
                //.centerCrop() // 居中裁剪
                //.apply(requestOptions)
                //.apply(RequestOptions.bitmapTransform(new BlurTransformation(20)))//模糊处理
                //.apply(RequestOptions.bitmapTransform(new GrayscaleTransformation()))//黑白处理
                //.override(50, 50)
                //.transition(DrawableTransitionOptions.withCrossFade(factory))//定义 Glide 如何从占位符到新加载的图片
                //.transform(new CircleCrop()) // 圆形
                //.transform(new RoundedCorners(100)) //圆角
                //.transform(new GranularRoundedCorners(20, 30, 40, 50))//四个角分别设置圆角
                //.transform(new Rotate(90)) //旋转
                //.transform(new BlurTransformation())//模糊处理
                //.transform(new GrayscaleTransformation())//自定义
                //.skipMemoryCache(true)//不使用内存缓存
                //.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                .into(mImage);

        Glide.with(this)
                //.asGif()
                //.asBitmap()
                .load("https://p7.itc.cn/q_70/images03/20200717/1ed39c71685a4d6bbc88dbb2e2a1a0f6.gif")
                //.load(R.drawable.ic_launcher_background)
                .into(mImage2);
    }

    RequestOptions requestOptions = new RequestOptions()//提取出来供多个请求使用
            .placeholder(R.mipmap.loding)
            .error(R.mipmap.error)
            .fallback(R.drawable.ic_launcher_foreground);


    DrawableCrossFadeFactory factory =
            new DrawableCrossFadeFactory.Builder().setCrossFadeEnabled(true).build();


    public class GrayscaleTransformation extends BitmapTransformation {

        public GrayscaleTransformation() {
            super();
        }

        @Override
        protected Bitmap transform(@NonNull Context context, @NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
            Bitmap.Config originalConfig = toTransform.getConfig();

            // 创建一个新的Bitmap，其颜色格式与原始Bitmap相同
            Bitmap bitmap = toTransform.copy(originalConfig, true);

            // 应用灰度效果
            ColorMatrix colorMatrix = new ColorMatrix();
            colorMatrix.setSaturation(0);
            ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
            Paint paint = new Paint();
            paint.setColorFilter(colorFilter);

            // 在新的Bitmap上绘制修改后的图像
            Bitmap mutableBitmap = bitmap.isMutable() ? bitmap : bitmap.copy(originalConfig, true);
            Canvas canvas = new Canvas(mutableBitmap);
            canvas.drawBitmap(toTransform, 0, 0, paint);

            // 返回修改后的Bitmap
            return mutableBitmap;
        }

        @Override
        public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {

        }

        @Override
        public boolean equals(Object o) {
            return false;
        }

        @Override
        public int hashCode() {
            return 0;
        }
    }
}